#!/bin/bash

# Jenkins 安装脚本
# 适用于 Ubuntu/Debian 系统

set -e

echo "开始安装 Jenkins..."

# 更新系统
echo "更新系统包..."
sudo apt update

# 安装 Java
echo "安装 Java..."
sudo apt install -y openjdk-11-jdk

# 添加 Jenkins 仓库
echo "添加 Jenkins 仓库..."
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

# 更新包列表
sudo apt update

# 安装 Jenkins
echo "安装 Jenkins..."
sudo apt install -y jenkins

# 启动 Jenkins 服务
echo "启动 Jenkins 服务..."
sudo systemctl start jenkins
sudo systemctl enable jenkins

# 等待 Jenkins 启动
echo "等待 Jenkins 启动..."
sleep 30

# 显示初始密码
echo "Jenkins 安装完成！"
echo "初始密码："
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

echo ""
echo "请访问: http://$(hostname -I | awk '{print $1}'):8080"
echo "使用上述密码完成初始化设置" 